<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li{
            width: 500px;
            height: 50px;
            background-color:firebrick;
            margin-bottom: 50px;
        }
    </style>
</head>
<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
</html>
<script>
    var list = document.querySelectorAll('li')
    // for(let i=0;i<list.length;i += 1){
    //     list[i].onclick=function(){
    //         console.log(i);
    //     }
    // }
    // console.log(i);
    for(var i=0;i<list.length;i += 1){
        fn(i)
    }
    function fn(index){
        list[index].onclick=function(){
            console.log(index);
        }
    }
</script>